package com.example.travelservlet.entertainmentPlan.controller;

import com.example.travelservlet.entertainment.service.EntertainmentService;
import com.example.travelservlet.entertainmentPlan.service.EntertainmentPlanService;
import com.example.travelservlet.entity.Entertainment;
import com.example.travelservlet.entity.EntertainmentPlan;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author hxy
 * @date 2022/11/9 14:52
 */
@RestController
@RequestMapping("/entertainmentPlan")
public class EntertainmentPlanController {
    @Autowired
    private EntertainmentPlanService entertainmentPlanService;
    @Autowired
    private EntertainmentService entertainmentService;



    @PostMapping("/createEntertainmentPlan")
    public Result<EntertainmentPlan> createEntertainmentPlan(@Valid EntertainmentPlan entertainmentPlan,@Valid String beginTime, @Valid String endTime) {
        for(EntertainmentPlan e:entertainmentPlanService.findEntertainmentPlansByPlanId(entertainmentPlan.getPlanId())){
            if (e.getSequence().equals(entertainmentPlan.getSequence())){
                entertainmentPlanService.deleteEntertainPlan(e);
            }
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        try{
            if (beginTime == null) {
                entertainmentPlan.setBeginTime(dateFormat.format(new Date()));
            } else {
                Date begin = dateFormat.parse(beginTime+":00");
                entertainmentPlan.setBeginTime(dateFormat.format(begin));
            }
            if (endTime == null) {
                entertainmentPlan.setEndTime(dateFormat.format(new Date()));
            } else {
                Date end = dateFormat.parse(endTime+":00");
                entertainmentPlan.setEndTime(dateFormat.format(end));
            }

        }catch (ParseException e) {
            throw new RuntimeException(e);
        }
            EntertainmentPlan save = entertainmentPlanService.createEntertainmentPlan(entertainmentPlan);
            return ResultUtil.success(save);
        }

    @PostMapping("/createEntertainmentPlans")
    public Result<List<EntertainmentPlan>> createEntertainmentPlans(@Valid List<EntertainmentPlan> entertainmentPlans) {
        List<EntertainmentPlan> entertainmentPlansList = entertainmentPlanService.createEntertainmentPlans(entertainmentPlans);
        return ResultUtil.success(entertainmentPlansList);
    }

    @GetMapping("/findEntertainmentWithPlan/{planId}")
    public Result<List<Entertainment>> findEntertainmentWithPlan(@PathVariable String planId) {
        List<EntertainmentPlan> entertainmentPlans = entertainmentPlanService.findEntertainmentPlansByPlanId(planId);
        List<String> entertainmentIds = new ArrayList<>();
        for (EntertainmentPlan entertainmentPlan : entertainmentPlans) {
            entertainmentIds.add(entertainmentPlan.getEntertainmentId());
        }

        Page pageResult = entertainmentService.findEntertainmentsByEntertainmentIds(entertainmentIds, null);
        return ResultUtil.success(pageResult);
    }

    @GetMapping("/deleteEntertainmentPlan/{planId}/{sequence}")
    public Result deleteEntertainment(@PathVariable String planId,@PathVariable Integer sequence) {
        EntertainmentPlan entertainmentPlan =entertainmentPlanService.findEntertainmentPlanByPlanIdAndSequence(sequence,planId);
        entertainmentPlanService.deleteEntertainPlan(entertainmentPlan);
        return ResultUtil.success("删除成功");
    }

    @GetMapping("/updateEntertainment/{entertainmentId}/{planId}")
    public Result<EntertainmentPlan> updateEntertainment(@PathVariable String entertainmentId, @PathVariable String planId) {
        EntertainmentPlan entertainmentPlan = new EntertainmentPlan();
        entertainmentPlan.setEntertainmentId(entertainmentId);
        entertainmentPlan.setPlanId(planId);
        EntertainmentPlan update = entertainmentPlanService.updateEntertainmentPlanByPlanId(entertainmentPlan);
        return ResultUtil.success(update);
    }

    @GetMapping("/findEntertainmentPlansByPlanId/{plaId}")
    public Result<List<EntertainmentPlan>> findEntertainmentPlansByPlanId(@PathVariable String plaId){
        List<EntertainmentPlan> entertainmentPlans = entertainmentPlanService.findEntertainmentPlansByPlanId(plaId);
        return ResultUtil.success(entertainmentPlans);
    }

}
